﻿using CodeCity.UserManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace CodeCity.UserMangament.Repository
{
    public class GameDbContext : DbContext
    {
        public GameDbContext([NotNullAttribute] DbContextOptions options) : base(options)
        {
        }

        public GameDbContext()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseNpgsql("server = localhost; DataBase = codecity_user; UserId = postgres; Password = 12345678");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var entityTypes = typeof(Human).Assembly.GetExportedTypes().Where(x => x.BaseType == typeof(ModelBase)).ToArray();
            foreach (var item in entityTypes)
            {
                if (modelBuilder.Model.FindEntityType(item) != null)
                {
                    continue;
                }
                modelBuilder.Model.AddEntityType(item);
            }
        }
    }
}
